// pages/personalCenter/order/order.js;
import { unix } from 'dayjs';
import { orderPage, orderUpdate,orderSell } from '../../../common/api/my'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navBarHeight: getApp().globalData.navBarHeight,
    statusBarHeight:getApp().globalData.statusBarHeight,
    goodsData:[1,3,3,3,3,],
    status: '',
    commodityArr: [],
    page: 1, // 页码
    orderNo: '', // 搜索订单号
    bottomLoading: true,
    isMore:false,
    statusObj: {
         1: '进行中',
        2: '待发货',
        3: '待收货',
        4: '已完成'
    }
  },
  onShareAppMessage(e) {
    let user = wx.getStorageSync('userInfo');
    console.log(e);
    return {
      title: "杆赢天下，求胜未来。一元乐享好球杆，千万球友的选择",
      imageUrl: "../../../images/shareImg.jpg",
       path: "/pages/index/index?id=" + user.id
    }
  },
  back:function(){
    wx.navigateBack();
  },
  onReachBottom(){
    if(this.data.isMore){
      console.log('没有更多了')
      return
    }
    this.data.page+=1;
    this.getShop();
  },
  go:function(e){
    let row = e.currentTarget.dataset.item;
    wx.navigateTo({
      url: '../orderDetail/orderDetail?id=' + row.id,
    })
  },
  onLoad(options){ 
    // 订单状态
    // {
    //   1: '进行中',
    //   2: '待发货',
    //   3: '待收货',
    //   4: '已完成'
    // }
    this.data.status = options.status == 'status' ? '': options.status;
    this.getShop();
  },
  getSeacrh: function(){
  
    this.bindreFresherrefresh()
   },
   // 获取订单历史
   getShop: async function () {
    let user = wx.getStorageSync('userInfo');
    let data = {
      status: this.data.status,
      page: this.data.page,
      size: 10,
      orderNo: this.data.orderNo,
      goodName: this.data.orderNo,
      userId: user.id
    };
    let res = await orderPage(data);
    // console.log(res.list, '商品列表')
    // 测试分页
    // let arr2 = [];
    // for(let i=0;i<10;i++){
    //   arr2.push(...res.list)
    // };
    // let arr = arr2 || [];
    let arr = res.list || [];
      if(res.isLastPage){
        this.setData({
          bottomLoading: false
        })
      };
    console.log(this.data.commodityArr, '数据')
    let a = [...this.data.commodityArr, ...arr];
    if(res.isLastPage){
      this.data.isMore = true;
    }
    this.setData({
      commodityArr: a,
      refresherTriggered: false
    });
  },
  // 置换事件
  async consignment(e){
    console.log("点击置换",e.currentTarget.dataset.item);
    let item = e.currentTarget.dataset.item;
    let user = wx.getStorageSync('userInfo');
    let data =  {userId:user.id, id: item.id};
    let res = await orderSell(data);
    wx.showModal({
      title: '置换成功',
      content: '',
      showCancel: "false",
      complete: (datas) => {
        if (datas.cancel) {
          
        }
    
        if (datas.confirm) {
          this.bindreFresherrefresh();
        }
      }
    })
    // this.bindreFresherrefresh();
  },
  async bindreFresherrefresh(){
    wx.showLoading()
    this.data.page = 1;
    this.setData({
      bottomLoading: true,
      commodityArr: []
    });
    await this.getShop();
    wx.hideLoading()
  },
  inputChange: function(e){
    this.data.orderNo = e.detail.value;
    console.log(this.data.orderNo, '搜索')
  },
  async confirmReceipt(e){
     
      let data = e.currentTarget.dataset.item;
      data.status = 4;
      let res = await orderUpdate(data);
      // console.log(data)
      this.bindreFresherrefresh();
  }
})